package com.enjoy.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.enjoy.R;
import com.enjoy.commons.Commons;
import com.enjoy.utils.GPS;
import com.enjoy.utils.HttpPostHandle;
import com.enjoy.utils.PreferenceHelper;

public class Login extends Activity{
	Button bt_login;
	Button bt_signup;
	EditText et_username;
	EditText et_password;
	String username;
	String password;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects() 
				.penaltyLog().penaltyDeath().build());
		
		bt_login = (Button)findViewById(R.id.bt_login_login);
		bt_signup = (Button)findViewById(R.id.bt_login_signup);
		et_username = (EditText)findViewById(R.id.et_login_username);
		et_password = (EditText)findViewById(R.id.et_login_password);
		
		if(PreferenceHelper.settingExists(this, "username") && PreferenceHelper.settingExists(this, "password")){
			et_username.setText(PreferenceHelper.getSetting(this, "username", ""));
			et_password.setText(PreferenceHelper.getSetting(this, "password", ""));
		}
		
		bt_login.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
//				Location location=GPS.getLocation(Login.this);
//				
//				bt_login.setText(location.getLongitude()+"  :"+location.getLatitude());
//				
				
				
//TODO增加EVENT				HttpPostHandle httphandle = new HttpPostHandle(Commons.URL
//						+ "serviceName=EventService&operation=insert");
//				
//				httphandle.add("ename", "真是开a");
//				httphandle.add("uid", "1");
//				httphandle.add("content", "content");
//				httphandle.add("stime", "stime");
//				httphandle.add("duration", "1.5");
//				httphandle.add("latitude", "22.2");
//				httphandle.add("longitude", "144.4");
//				httphandle.add("status", "movie");
//				httphandle.add("placename", "placename");
//				
//				
//				
//				
//				
//				
//				JSONObject  JSON = httphandle.getResponse();
//				
//				bt_login.setText(JSON.toString());
				
				
				username=et_username.getText().toString();
				password=et_password.getText().toString();
				
				HttpPostHandle httphandle = new HttpPostHandle(Commons.URL
						+ "serviceName=UserService&operation=identification");
				
				httphandle.add("username", username);
				httphandle.add("password", password);
				
				
				
				
				JSONObject  JSON = httphandle.getResponse();
				try {
					if(JSON !=null && JSON.getInt("errorcode")== 0){
						Toast.makeText(getApplicationContext(), "登陆成功",
							     Toast.LENGTH_SHORT).show();
						
						Log.i("uid",""+JSON.getJSONObject("result").getInt("uid"));
						PreferenceHelper.addSetting(Login.this, "uid", JSON.getJSONObject("result").getInt("uid")+"");
						
						PreferenceHelper.addSetting(Login.this, "username", username);
						PreferenceHelper.addSetting(Login.this, "password", password);
						
						Intent intent = new Intent();
						intent.setClass(Login.this, Welcome.class);
						startActivity(intent);
						
						
					}else{
						Toast.makeText(getApplicationContext(), "用户名密码组合错误",
							     Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
		
		bt_signup.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Login.this,
						SignUp.class);
				Login.this.startActivity(intent);

			}
		});
		
		
		
		
		
	}
}
